package gengine.clientside.forms;

import gengine.clientside.ClientApplication;
import gengine.common.packets.BasePacket;
import gengine.common.packets.PacketProcessor;
import gengine.common.packets.PacketTypeEnum;
import gengine.common.packets.PingPacket;

import javax.swing.*;
import java.awt.*;

/**
 * Date: 04.03.13
 * Time: 20:20
 *
 * @author Zont
 */
public class ClientInfoPanel extends JPanel implements PacketProcessor {
    JButton tfPing;
    JLabel tfId;
    JTextField tfName;
    private ClientApplication app;

    public ClientInfoPanel(ClientApplication app) {
        super(new GridLayout(), true);
        this.app = app;

        //Name
        JLabel label = new JLabel("Name");
        label.setName("lbName");
        tfName = new JTextField("Not set");
        tfName.setName("tfName");
        tfName.addActionListener((e)->{
            app.clientInfo.setName(tfName.getName());
            app.sendClientInfo();
        }
        );
        add(label);
        add(tfName);

        //Id
        label = new JLabel("id");
        label.setName("lbId");
        tfId = new JLabel("Id");
        tfId.setName("lbId");
        add(label);
        add(tfId);
        label = new JLabel("Ping");
        label.setName("lbPing");
        add(label);
        tfPing = new JButton("Unknown");
        tfPing.setName("tfPing");
        tfPing.addActionListener((e)->{
            app.clientThread.sendPacketImmediately(new PingPacket());
        });
        add(tfPing);
        app.Subscribe(this, PacketTypeEnum.Ping);
    }

    @Override
    public boolean Process(BasePacket packet) {
        switch (packet.getPacketType()) {
            case Ping:
                tfPing.setText(Long.toString(System.currentTimeMillis()-((PingPacket)packet).getCreationTime()));
                return true;
        }
        return false;
    }
}
